<%@page contentType="text/html; charset=utf-8"%>
<%@ include file="/commons/taglibs.jsp"%>
<%@ include file="/commons/commonEdit.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript"
	src="${ctx}/scripts/tags/unitedselect/UnitedSelect.js"></script>
<script type="text/javascript" src="${ctx}/scripts/common.js"></script>
<script language="JavaScript" src="${ctx}/scripts/common/prototype.js"></script>
<script type="text/javascript"
	src="${ctx}/scripts/validation/validation.js"></script>
<script type="text/javascript" src="${ctx}/scripts/common.js"></script>
<script language="JavaScript" src="${ctx}/scripts/div/common.js"></script>
<script type="text/javascript">
	var ACTION_URL="${ctx}/charge_charStoChoAction.do?";

	function onSelectChargeItem(thiz)
	{
		selectId = thiz.options[thiz.selectedIndex].value;
		var url =ACTION_URL+"method=unitedSelect&selectId="+selectId+"&targetId=chargeStaID";
		onChangeSelect(url,selectId,'chargeStaID');
	}

	function onSelectMeterType(thiz)
	{
		selectId = thiz.options[thiz.selectedIndex].value;
		var url =ACTION_URL+"method=unitedSelect&selectId="+selectId+"&targetId=chargeItemID";
		onChangeSelect(url,selectId,'chargeItemID');
	}

	function clearSelectTable(){
		document.frames.iChoosehouse.clearSelectTable();
	}

	function changeSelectHouseStatus(){
		if($F('chargeItemID')=="" || $F('meterId')==""){
			document.frames.iChoosehouse.$('tdSelectAll').disabled=true;
			document.frames.iChoosehouse.$('buildingCellId').disabled=true;
		}else{
			document.frames.iChoosehouse.$('tdSelectAll').disabled=false;
			document.frames.iChoosehouse.$('buildingCellId').disabled=false;
		}
	}

	function getHouseIdByTable(){
		return document.frames.iChoosehouse.getHouseIdByTable();
	}

	//新增
	function add(){
		var url= ACTION_URL + "method=addMeterCho";
		var pars=$('form1').serialize()+"&houseIds="+getHouseIdByTable();
		//校验
		var isTure= $("form1").fireEvent("onSubmit");
		//ajax提交
		if(isTure && checkSelectHouse()){
			closeORopenButton();
		//			alert(url);
			var myAjax = new Ajax.Request(
		                    url,
		                    {method: 'post', parameters: pars, onComplete: 	function (result){
								var obj=findFrame('main');
								obj.reLoadIFrameList();
								closeORopenButton();
								ajaxSubmitException(result.responseText);
								windowClose();}
		                    });
		}
	}

	//验证：当某个房号、项目相同时，checkExistCharStoChoResult改为true,并提示错误信息
	var checkExistCharStoChoResult=false;
	function checkExistCharSto(houseId,houseNumber){
		if(!houseId || !houseNumber){
			return ;
		}

		var url= ACTION_URL + "method=checkExistCharStoChoMeter";
//		var pars="&houseId="+houseId+"&chargeStaID="+staId+"&chargeItemID="+itemId+"&meterId="+meterId;
		var pars=$('form1').serialize()+"&houseId="+houseId;
		//ajax提交
			var myAjax = new Ajax.Request(
		                    url,
		                    {method: 'post', parameters: pars,asynchronous:false, onComplete: 	function (result){
								if(result.responseText=="false"){
									checkExistCharStoChoResult=false;
								}else{
									alert(houseNumber+result.responseText);
									checkExistCharStoChoResult=true;
								}
								}
		                    });

	}
	function addAgain(){
		var url= ACTION_URL + "method=addMeterCho";
		var pars=$('form1').serialize()+"&houseIds="+getHouseIdByTable();
		//校验
		var isTure= $("form1").fireEvent("onSubmit");
		//ajax提交
		if(isTure && checkSelectHouse()){
			closeORopenButton();
			//	alert(url);
			var myAjax = new Ajax.Request(
		                    url,
		                    {method: 'post', parameters: pars, onComplete: 	function (result){
								var obj=findFrame('main');
								obj.reLoadIFrameList();
								closeORopenButton();
								ajaxSubmitException(result.responseText);
								document.location.reload();}
		                    });
		}
	}

	function checkSelectHouse(){
		if(getHouseIdByTable()==""){
			document.frames.iChoosehouse.$("selectSpanMessage").style.display="";
			return false;
		}else{
			document.frames.iChoosehouse.$("selectSpanMessage").style.display="none";
			return true;
		}
	}

	//关闭或打开按钮事件
	function closeORopenButton(){
		$$('window_footer').disabled= !$$('window_footer').disabled;
		$('btn1_22').disabled= !$('btn1_22').disabled;
		$('btn1_32').disabled= !$('btn1_32').disabled;
		$('btn1_42').disabled= !$('btn1_42').disabled;
	}

	//存放控件的原始值
	var sourceValue="";

	function closeWin(){
		var newValue=$('form1').serialize()+getHouseIdByTable();
		if(sourceValue==newValue){
			windowClose();
		}else{
			var result=confirm("${key.tipsForCloseWindow}");
			if(result){
				add();
				return;
			}else{
				windowClose();
			}
		}
	}

	function init(){
		$('iChoosehouse').src=ACTION_URL+"method=initChooseHouse";
		sourceValue=$('form1').serialize();
	}

</script>
</head>

<body onload="init();">
<form action="" id="form1" class="required-validate" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td class="window_head_outBg">
		<table border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td><span class="window_head_intext"><img
					src="${ctx}/images/ico_Advance.gif" align="absmiddle">&nbsp;${key.addCharge}</span></td>
			</tr>
		</table>
		</td>
	</tr>
	<tr>
		<td class="aux_header_line_1"></td>
	</tr>
	<tr>
		<td class="aux_header_line_2"></td>
	</tr>

	<tr>
		<td class="content">
		<table width="100%" border="0" align="center" cellpadding="0"
			cellspacing="0" class="outtable">
			<tr>
				<td>
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td class="aux_outtable_color">
						<table width="100%" border="0" cellspacing="0" cellpadding="0">
							<tr>
								<td>

								<table width="100%" border="0" cellpadding="0" cellspacing="1"
									class="aux_intable_color">
									<tr>
								         <td width="13%" class="aux_intable_tdwidwidth">${key.meterType}：</td>
								         <td width="13%" class="aux_intable_tdkeyword">
										<pms:unitedSelect
											id="meterId" name="meterTypeId" dataList="${meterTypeList}"
											optionLabel="name" optionValue="code" className="required"
											onChange="onSelectMeterType(this);changeSelectHouseStatus();clearSelectTable();"
											selectText="${key.pleaseSelect}"
											targetId="chargeItemID"
										>
										</pms:unitedSelect></td>
										<td class="aux_intable_tdwidwidth">${key.chargeItem}：</td>
										<td class="aux_intable_tdkeyword">
										<pms:unitedSelect
											id="chargeItemID" name="chargeItemID"
											targetId="chargeStaID"
											optionLabel="itemName" optionValue="keyword"
											onChange="onSelectChargeItem(this);changeSelectHouseStatus();clearSelectTable();"
											selectText="${key.pleaseSelect}"
											className="required">
										</pms:unitedSelect></td>
										<td class="aux_intable_tdwidwidth">${key.chargeSta}：</td>
										<td class="aux_intable_tdkeyword">
										<pms:unitedSelect
											id="chargeStaID" name="chargeStaID" selectText="${key.pleaseSelect}"
											className="required" >
										</pms:unitedSelect></td>
									</tr>
									<tr>
										<td colspan="6" class="aux_intable_td1"><iframe
											id="iChoosehouse" frameborder="0" height="310" width="100%"
											scrolling="no" src=""></iframe></td>
									</tr>
								</table>
								</td>
							</tr>
						</table>
						</td>
					</tr>
				</table>
				</td>
			</tr>
		</table>
		</td>
	</tr>
	<tr>
		<td colspan="2" class="window_footer">
		<table border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td>
				<table border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td class="buttonOffLeft" id="btn1_41"></td>
						<td class="buttonOffMiddle" id="btn1_42" onClick="addAgain();"
							onMouseOver="btn_change_over(4)" onMouseOut="btn_change_out(4)"
							onblur="btn_change(4)"><img
							src="${ctx}/images/ico_saveandnewfile.gif" alt="" width="14"
							height="14" border="0" align="absbottom"> ${key.saveAndAdd}</td>
						<td class="buttonOffRight" id="btn1_43"></td>
					</tr>
				</table>
				</td>
				<td class="btn_between_space">&nbsp;</td>
				<td>
				<table border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td class="buttonOffLeft" id="btn1_21"></td>
						<td class="buttonOffMiddle" id="btn1_22" onClick="add();"
							onMouseOver="btn_change_over(2)" onMouseOut="btn_change_out(2)"
							onBlur="btn_change(2)"><img src="${ctx}/images/ico_ok.gif"
							alt="" width="14" height="14" border="0" align="absbottom">
						${key.save}</td>
						<td class="buttonOffRight" id="btn1_23"></td>
					</tr>
				</table>
				</td>
				<td class="btn_between_space"></td>
				<td>
				<table border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td class="buttonOffLeft" id="btn1_31"></td>
						<td class="buttonOffMiddle" id="btn1_32" onClick="closeWin();"
							onMouseOver="btn_change_over(3)" onMouseOut="btn_change_out(3)"
							onBlur="btn_change(3)"><img
							src="${ctx}/images/ico_cancel.gif" alt="" width="14" height="14"
							border="0" align="absbottom"> ${key.close} </td>
						<td class="buttonOffRight" id="btn1_33"></td>
					</tr>
				</table>
				</td>
			</tr>
		</table>
		<p>&nbsp;</p>
		</td>
	</tr>
</table>

</form>
</body>
</html>
